/*
 * Copyright (c) 2016 moondev.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.moondev.moonlog.example;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 应用启动入口
 * @author CHEN(luecsc@sina.com)
 * @version 1.0.1
 * @date 16-1-29
 * @since 1.0.1
 */
public class Main {

    private static final Logger log = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {

        try {
            Server server = new Server();
            ClassPathResource configPath = new ClassPathResource("config.properties");
            Properties p = new Properties(System.getProperties());
            p.load(configPath.getInputStream());
            ClassPathResource classpath = new ClassPathResource("jetty.xml");
            XmlConfiguration xmlConfig = new XmlConfiguration(classpath.getInputStream());
            Map<String, String> props = new HashMap<String, String>();
            for (Object key : p.keySet()) {
                props.put(key.toString(), String.valueOf(p.get(key)));
            }
            xmlConfig.getProperties().putAll(props);
            xmlConfig.configure();
            xmlConfig.configure(server);

            WebAppContext webAppContext = new WebAppContext();
            FileSystemResource webappFile = new FileSystemResource("moonlog-example/webapp/");
            if (!webappFile.exists()) {
                webappFile = new FileSystemResource("../webapp/");
            }
            if (!webappFile.exists()) {
                throw new Exception("webapp path is wrong!");
            }
            webAppContext.setResourceBase(webappFile.getFile().getCanonicalPath());
            webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
            webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.redirectWelcome", "false");
            webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.welcomeServlets", "exact");
            webAppContext.setContextPath("/");
            webAppContext.setWelcomeFiles(new String[]{"index.htm"});
            server.setHandler(webAppContext);
            server.start();
            log.info("moonlog-example start sucess!");
            server.join();

        } catch (Exception e) {
            log.error("moonlog-example start fail!", e);
            System.exit(0);
        }
    }
}
